<?php
class RegistController extends Zend_Controller_Action
{
	public function init(){
		//set layout
    	$layoutPath = APPLICATION_PATH  . '/templates/default/default';
		$option = array (	'layout' 		=> 'index', 
              	        'layoutPath' 	=> $layoutPath );
		Zend_Layout::startMvc ( $option );
	}
	
    public function indexAction ()
    {
    	//TODO 必要なクラスを読み込む
    	Zend_Loader::loadClass("UserModel");
    	Zend_Loader::loadClass("MailModel");
    	Zend_Loader::loadClass("LogModel");
    	
    	$logModel = new LogModel();
    	$log_line = $logModel->getLogLine();
    	
    	$request = $this->getRequest();
    	$view = $this->view;
    	
    	if($request->isPost()){
    		//TODO パラメターを習得
	    	$username = $request->getParam("username");
	    	$password = $request->getParam("password");
	    	$name = $request->getParam("name");
	    	$email = $request->getParam("email");
	    	$phone = $request->getParam("phone");
	    	$address = $request->getParam("address");
	    	$type = NORMAL_USER;
	    	$status = STATUS_WAITING;
	    	
    		//TODO ユーザを追加
    		$model = new UserModel();
    		$password = hash("SHA1", $password);
    		$data = array(
    			'username' => $username,
    			'password' => $password,
    			'name' => $name,
    			'email' => $email,
    			'phone' => $phone,
    			'type' => $type,
    			'address' => $address,
    			'status' => $status	
    		);
    		
    		$check = $model->addUserData($data);
    		$this->_redirect("/regist/result?result=$check");
    	}
    }
    
    public function resultAction(){
    	
    }
    
public function isEmailExistAction ()
    {
    	Zend_Loader::loadClass("UserModel");
    	$request = $this->getRequest();
    	$model = new UserModel();
    	
    	if ($model->isEmailExist($request->getParam("email"))) {
    		echo "false";
    		exit;
    	}
    	
    	echo "true";
    	exit();
    }
    
    /**
     * ユーザがありましたか
     */
    public function isUsernameExistAction()
    {
    	Zend_Loader::loadClass("UserModel");
    	$request = $this->getRequest();
    	$model = new UserModel();
    	
    	if ($model->isUsernameExist($request->getParam("username"))){
    		echo "false";
    		exit();
    	}
    	echo "true";
    	exit();
    }
}